package org.nstack.header.storage.snapshot;

import org.nstack.header.query.*;
import org. nstack.header.query.*;
import org.nstack.header.search.Inventory;
import org.nstack.header.storage.primary.PrimaryStorageInventory;
import org.nstack.header.volume.VolumeInventory;

import javax.persistence.JoinColumn;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Inventory(mappingVOClass = VolumeSnapshotVO.class)
@ExpandedQueries({
        @ExpandedQuery(expandedField = "volume", inventoryClass = VolumeInventory.class,
                foreignKey = "volumeUuid", expandedInventoryKey = "uuid"),
        @ExpandedQuery(expandedField = "tree", inventoryClass = VolumeSnapshotTreeInventory.class,
                foreignKey = "treeUuid", expandedInventoryKey = "uuid"),
        @ExpandedQuery(expandedField = "primaryStorage", inventoryClass = PrimaryStorageInventory.class,
                foreignKey = "primaryStorageUuid", expandedInventoryKey = "uuid"),
        @ExpandedQuery(expandedField = "backupStorageRef", inventoryClass = VolumeSnapshotBackupStorageRefInventory.class,
                foreignKey = "uuid", expandedInventoryKey = "backupStorageUuid"),
})
@ExpandedQueryAliases({
        @ExpandedQueryAlias(alias = "backupStorage", expandedField = "backupStorageRef.backupStorage")
})
public class VolumeSnapshotInventory {

    private String uuid;

    private String name;

    private String description;

    private String type;

    private String volumeUuid;

    private String treeUuid;


    private String parentUuid;

    private String primaryStorageUuid;

    private String primaryStorageInstallPath;

    private String volumeType;

    private String format;

    private Boolean latest;

    private Long size;

    private String state;

    private String status;

    private Timestamp createDate;

    private Timestamp lastOpDate;

    @Queryable(mappingClass = VolumeSnapshotBackupStorageRefInventory.class,
            joinColumn = @JoinColumn(name = "volumeSnapshotUuid"))
    private List<VolumeSnapshotBackupStorageRefInventory> backupStorageRefs;

    public static VolumeSnapshotInventory valueOf(VolumeSnapshotVO vo) {
        VolumeSnapshotInventory inv = new VolumeSnapshotInventory();
        inv.setName(vo.getName());
        inv.setCreateDate(vo.getCreateDate());
        inv.setDescription(vo.getDescription());
        inv.setLastOpDate(vo.getLastOpDate());
        inv.setParentUuid(vo.getParentUuid());
        inv.setState(vo.getState().toString());
        inv.setType(vo.getType());
        inv.setVolumeUuid(vo.getVolumeUuid());
        inv.setFormat(vo.getFormat());
        inv.setUuid(vo.getUuid());
        inv.setStatus(vo.getStatus().toString());
        inv.setPrimaryStorageUuid(vo.getPrimaryStorageUuid());
        inv.setPrimaryStorageInstallPath(vo.getPrimaryStorageInstallPath());
        inv.setLatest(vo.isLatest());
        inv.setSize(vo.getSize());
        inv.setVolumeType(vo.getVolumeType());
        inv.setTreeUuid(vo.getTreeUuid());
        inv.setBackupStorageRefs(VolumeSnapshotBackupStorageRefInventory.valueOf(vo.getBackupStorageRefs()));
        return inv;
    }

    public static List<VolumeSnapshotInventory> valueOf(Collection<VolumeSnapshotVO> vos) {
        List<VolumeSnapshotInventory> invs = new ArrayList<VolumeSnapshotInventory>();
        for (VolumeSnapshotVO vo : vos) {
            invs.add(VolumeSnapshotInventory.valueOf(vo));
        }
        return invs;
    }

    public List<VolumeSnapshotBackupStorageRefInventory> getBackupStorageRefs() {
        return backupStorageRefs;
    }

    public boolean isOnBackupStorage(String backupStorageUuid) {
        for (VolumeSnapshotBackupStorageRefInventory ref : backupStorageRefs) {
            if (ref.getBackupStorageUuid().equals(backupStorageUuid)) {
                return true;
            }
        }

        return false;
    }

    public void setBackupStorageRefs(List<VolumeSnapshotBackupStorageRefInventory> backupStorageRefs) {
        this.backupStorageRefs = backupStorageRefs;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getTreeUuid() {
        return treeUuid;
    }

    public void setTreeUuid(String treeUuid) {
        this.treeUuid = treeUuid;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getVolumeUuid() {
        return volumeUuid;
    }

    public void setVolumeUuid(String volumeUuid) {
        this.volumeUuid = volumeUuid;
    }

    public String getParentUuid() {
        return parentUuid;
    }

    public void setParentUuid(String parentUuid) {
        this.parentUuid = parentUuid;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Timestamp getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    public Timestamp getLastOpDate() {
        return lastOpDate;
    }

    public void setLastOpDate(Timestamp lastOpDate) {
        this.lastOpDate = lastOpDate;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getPrimaryStorageUuid() {
        return primaryStorageUuid;
    }

    public void setPrimaryStorageUuid(String primaryStorageUuid) {
        this.primaryStorageUuid = primaryStorageUuid;
    }

    public String getPrimaryStorageInstallPath() {
        return primaryStorageInstallPath;
    }

    public void setPrimaryStorageInstallPath(String primaryStorageInstallPath) {
        this.primaryStorageInstallPath = primaryStorageInstallPath;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public String getVolumeType() {
        return volumeType;
    }

    public void setVolumeType(String volumeType) {
        this.volumeType = volumeType;
    }

    public boolean isLatest() {
        return latest;
    }

    public void setLatest(boolean latest) {
        this.latest = latest;
    }
}
